package com.hqyj.freemovie.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.freemovie.front.domain.Movie;
import com.hqyj.freemovie.front.mapper.MovieMapper;
import com.hqyj.freemovie.front.repository.MovieViewRepository;
import com.hqyj.freemovie.front.service.MovieService;
import com.hqyj.freemovie.front.view.MovieView;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements MovieService {

    @Resource
    private RestHighLevelClient restHighLevelClient;

    @Resource
    private MovieMapper movieMapper;


    @Resource
    private MovieViewRepository movieViewRepository;

    @Value("${freemovie.perPageSize}")
    private Integer perPagerSize;

    @Override
    public List<Movie> queryMovieListByType(String type) {
        QueryWrapper<Movie> queryWrapper = null;
        if (!"全部".equals(type)) {
            queryWrapper = new QueryWrapper<Movie>();
            queryWrapper.eq("type", type);
        }

        return movieMapper.selectList(queryWrapper);
    }


    @Override
    public Page<Movie> queryMoviePagerByType(String type, Integer currentPageNumber) {
        QueryWrapper<Movie> queryWrapper = null;
        if (!"全部".equals(type)) {
            queryWrapper = new QueryWrapper<Movie>();
            queryWrapper.eq("type", type);
        }
        Page<Movie> page = new Page(currentPageNumber, perPagerSize);

        return movieMapper.selectPage(page, queryWrapper);
    }


    @Override
    public boolean initElasticsearchMoviesLiberay() {
        try {
            List<Movie> movieList = queryMovieListByType("全部");
            //把movie集合转换为movieview的集合
            List<MovieView> movieViewList = new ArrayList<MovieView>();
            for (Movie m : movieList) {
                MovieView movieView = new MovieView();
                movieView.setId(m.getId());
                movieView.setName(m.getName());
                movieView.setDirector(m.getDirector());
                movieView.setDuration(m.getDuration());
                movieView.setLogo(m.getLogo());
                movieView.setIntroduce(m.getIntroduce());
                movieView.setType(m.getType());
                movieView.setUrl(m.getUrl());
                movieView.setPlayDate(m.getPlayDate());
                movieViewList.add(movieView);
            }
            movieViewRepository.saveAll(movieViewList);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    private boolean checkIndexIsExist(String indexName) throws Exception {
        GetIndexRequest indexRequest = new GetIndexRequest().indices(indexName);
        boolean exists = restHighLevelClient.indices().exists(indexRequest, RequestOptions.DEFAULT);
        return exists;

    }

    @Override
    public org.springframework.data.domain.Page<MovieView> elasticsearchMoviesByKeyword(String keyword, Integer currentPageNumber) {

        //在做全文检索之前，判断一下elasticsearch有没有名字叫movie索引，如果没有，必须把电影资料初始化一下。
        //判断movie这个索引是否存在。
        try {
            if (!checkIndexIsExist("movie")) {
                initElasticsearchMoviesLiberay();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        Pageable pager = PageRequest.of(--currentPageNumber, perPagerSize);

        return movieViewRepository.elasticsearchMoviesByKeyWords(keyword, pager);
    }
}
